﻿--[[
   @brief    
   @author   rickonzhang
   @version  1.0
   @date     2015-08-25
--]]

right_corner_pop_wnd_view = {
    uilib_ = ui_utility.uilib();
    root_id_ = 0;
    dialog_ptr_ = nil;
    is_add_timer = false;
    id_to_item_ = {};
    action_btn_container_ = nil;
    item_container_ = nil;
    width = 0;
    height = 0;
    is_show = false;
    is_mouse_hover_on_wnd = false;
}

function right_corner_pop_wnd_view.init()
    ierd_tgp.tpf_ui.Require("social\\right_corner_pop_wnd\\pop_wnd_item.lua");
end

function right_corner_pop_wnd_view.show(bShow, x, y)
    right_corner_pop_wnd_view.create();
    if bShow then
        right_corner_pop_wnd_view.remove_time();
        if right_corner_pop_wnd_view.is_show == true then
            return;
        end
        tqos_mgr.increase_tray_hover_wnd_show_count();
        --right_corner_pop_wnd_view.dialog_ptr_:Show(TRUE);
        right_corner_pop_wnd_view.is_show = true;
        right_corner_pop_wnd_view.update_item_info();
        right_corner_pop_wnd_view.move(x, y);
    else
        if right_corner_pop_wnd_view.is_mouse_hover_on_wnd == false then
            right_corner_pop_wnd_view.add_timer();
        end
    end
end

function right_corner_pop_wnd_view.create()
    if right_corner_pop_wnd_view.dialog_ptr_ == nil then
        --local main_wnd_id = right_corner_pop_wnd_view.uilib_:GetControlIDbyCustomID(CID_MAIN_WINDOW);
        right_corner_pop_wnd_view.root_id_ = layout_mgr.restore_layout("social\\right_corner_pop_wnd.tpf");
        right_corner_pop_wnd_view.dialog_ptr_ =ui_utility.get_ctrl_ptr_by_id(right_corner_pop_wnd_view.root_id_);

        local ui_str, str_holder = U8toCUIStr("Container_1");
        local id = right_corner_pop_wnd_view.uilib_:GetControlIDbyPath(ui_str, right_corner_pop_wnd_view.root_id_);
        right_corner_pop_wnd_view.action_btn_container_ =ui_utility.get_ctrl_ptr_by_id(id);

        ui_str, str_holder = U8toCUIStr("Container_2");
        right_corner_pop_wnd_view.item_container_ = right_corner_pop_wnd_view.uilib_:GetControlIDbyPath(ui_str, right_corner_pop_wnd_view.root_id_);

        local highlight_color = "RGB:255,200,0";
        if base_utility.is_type_of_oversea_ver() then
            highlight_color = "RGB:25,178,255";
        end

        local btn_id = ui_utility.get_ctrl_id_by_path(right_corner_pop_wnd_view.root_id_, "Container_1\\Button_2");
        if nil ~= btn_id then
            ui_utility.setproperty_by_id(btn_id, "TextColor", highlight_color);
            ui_utility.setproperty_by_id(btn_id, "TextColorHover", highlight_color);
            ui_utility.setproperty_by_id(btn_id, "TextColorPressed", highlight_color);
        end
    end
end

function right_corner_pop_wnd_view.move(x, y) -- x y 是右下角图标icon相对屏幕的坐标
    -- body
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    local rc = tpfui:GetWindowsTaskbarPosition();

    local inter_space = 0;
    local pop_rc = right_corner_pop_wnd_view.dialog_ptr_:GetRectEx(TRUE,TRUE);
    if rc.left == 0 and rc.top == 0 then
       if rc.bottom > rc.right then
            base_utility.log("left");
            pop_rc.left = rc.right + inter_space;
            pop_rc.right = pop_rc.left + right_corner_pop_wnd_view.width;
            pop_rc.bottom = rc.bottom - inter_space;
            pop_rc.top = rc.bottom - right_corner_pop_wnd_view.height;

            --根据icon的图标做移动，尽量靠近中间
            local mid = 0;
            if right_corner_pop_wnd_view.height % 2 == 1 then
                mid = pop_rc.top + (right_corner_pop_wnd_view.height + 1) / 2;
            else
                mid = pop_rc.top + right_corner_pop_wnd_view.height / 2;
            end
            if mid > y then
                pop_rc.top = pop_rc.top - (mid - y);
                pop_rc.bottom = pop_rc.top + right_corner_pop_wnd_view.height;
            end
       else
            base_utility.log("top");
            pop_rc.right = rc.right - inter_space;
            pop_rc.left = pop_rc.right - right_corner_pop_wnd_view.width;
            pop_rc.top = rc.bottom + inter_space;
            pop_rc.bottom = pop_rc.top + right_corner_pop_wnd_view.height;

            local mid = 0;
            if right_corner_pop_wnd_view.width % 2 == 1 then
                mid = pop_rc.left + (right_corner_pop_wnd_view.width + 1) / 2;
            else
                mid = pop_rc.left + right_corner_pop_wnd_view.width / 2;
            end
            if mid > x then
                pop_rc.left = pop_rc.left - (mid - x);
                pop_rc.right = pop_rc.left + right_corner_pop_wnd_view.width;
            end
       end
    elseif rc.left == 0 and rc.top ~= 0 then
            base_utility.log("bottom");
            pop_rc.left = rc.right - right_corner_pop_wnd_view.width - inter_space;
            pop_rc.right = pop_rc.left + right_corner_pop_wnd_view.width;
            pop_rc.bottom = rc.top - inter_space;
            pop_rc.top = pop_rc.bottom - right_corner_pop_wnd_view.height;

            local mid = 0;
            if right_corner_pop_wnd_view.width % 2 == 1 then
                mid = pop_rc.left + (right_corner_pop_wnd_view.width + 1) / 2;
            else
                mid = pop_rc.left + right_corner_pop_wnd_view.width / 2;
            end
            if mid > x then
                pop_rc.left = pop_rc.left - (mid - x);
                pop_rc.right = pop_rc.left + right_corner_pop_wnd_view.width;
            end
    else
            base_utility.log("right");
            pop_rc.right = rc.left - inter_space;
            pop_rc.left = pop_rc.right - right_corner_pop_wnd_view.width;
            pop_rc.bottom = rc.bottom - inter_space;
            pop_rc.top = pop_rc.bottom - right_corner_pop_wnd_view.height;

            local mid = 0;
            if right_corner_pop_wnd_view.height % 2 == 1 then
                mid = pop_rc.top + (right_corner_pop_wnd_view.height + 1) / 2;
            else
                mid = pop_rc.top + right_corner_pop_wnd_view.height / 2;
            end
            if mid > y then
                pop_rc.top = pop_rc.top - (mid - y);
                pop_rc.bottom = pop_rc.top + right_corner_pop_wnd_view.height;
            end
    end

    --相对主窗口做位置移动
    --[[local main_wnd = ui_utility.get_ctrl_ptr_by_cid(CID_MAIN_WINDOW);
    local main_wnd_rc = main_wnd:GetRectEx(TRUE,TRUE);
    pop_rc.right = pop_rc.left - main_wnd_rc.left + (pop_rc.right - pop_rc.left);
    pop_rc.left = pop_rc.left - main_wnd_rc.left;
    pop_rc.bottom = pop_rc.top - main_wnd_rc.top + (pop_rc.bottom - pop_rc.top);
    pop_rc.top = pop_rc.top - main_wnd_rc.top;]]
    right_corner_pop_wnd_view.dialog_ptr_:Move(pop_rc);
    right_corner_pop_wnd_view.dialog_ptr_:Show(TRUE);

end

function right_corner_pop_wnd_view.hide(uilib, ctrl_id)
    layout_mgr.delay_remove_ctrl(right_corner_pop_wnd_view.root_id_);
    right_corner_pop_wnd_view.root_id_ = 0;
    right_corner_pop_wnd_view.dialog_ptr_ = nil;
    right_corner_pop_wnd_view.action_btn_container_ = nil;
    right_corner_pop_wnd_view.item_container_ = nil;
    right_corner_pop_wnd_view.is_show = false;
end

function right_corner_pop_wnd_view.on_mouse_hover(uilib, ctrl_id)
    right_corner_pop_wnd_view.remove_time();
    right_corner_pop_wnd_view.is_mouse_hover_on_wnd = true;
end

function right_corner_pop_wnd_view.on_mouse_leave(uilib, ctrl_id)
    if right_corner_pop_wnd_view.dialog_ptr_ and
        right_corner_pop_wnd_view.dialog_ptr_:IsVisible() == TRUE then
        right_corner_pop_wnd_view.add_timer();
        right_corner_pop_wnd_view.is_mouse_hover_on_wnd = false;
    end
end

function right_corner_pop_wnd_view.add_timer()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    if right_corner_pop_wnd_view.is_add_timer == true then
        base_utility.log("[right_corner_pop_wnd_view]add_timer timer is already exist, delete first");
        tpfui:RemoveInterval('right_corner_pop_wnd_view.on_restore_interval');
    end

    base_utility.log("[right_corner_pop_wnd_view]add timer");
    right_corner_pop_wnd_view.is_add_timer = true;
    tpfui:AddInterval('right_corner_pop_wnd_view.on_restore_interval', 700);
end

function right_corner_pop_wnd_view.remove_time()
    if right_corner_pop_wnd_view.is_add_timer == false then
        base_utility.log("[right_corner_pop_wnd_view]remove_time timer is not exist");
        return;
    end
    base_utility.log("[right_corner_pop_wnd_view]remove timer");
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('right_corner_pop_wnd_view.on_restore_interval');
    right_corner_pop_wnd_view.is_add_timer = false;
end

function right_corner_pop_wnd_view.on_restore_interval()
    right_corner_pop_wnd_view.hide_immediately();
end

function right_corner_pop_wnd_view.hide_immediately()
    if right_corner_pop_wnd_view.dialog_ptr_ then
        base_utility.log("[right_corner_pop_wnd_view]hide_immediately");
        right_corner_pop_wnd_view.remove_time();
        right_corner_pop_wnd_view.dialog_ptr_:Show(FALSE)
    end
end

function right_corner_pop_wnd_view.update_item_info()
    right_corner_pop_wnd_view.delete_all_sub_items();
    local msg_array = {};
    if pwg_social_control:is_new_social_enabled() then
        msg_array = pwg_social_model:get_last_message();
    else
        msg_array = right_corner_pop_wnd_view.get_last_message()
    end
    local x = 0;
    local y = 0;
    local loop_cnt = 0;
    local max_show_msg_count = 3;

    if next(msg_array) then
        for index, msg in pairs(msg_array) do
            local item = pop_wnd_item:new(right_corner_pop_wnd_view.item_container_, msg, pop_wnd_item.TALK_MSG_TYPE);
            local id = item:get_item_root_id();
            right_corner_pop_wnd_view.id_to_item_[id] = item;

            item:move(x, y);
            local w, h = item:get_size();
            y = y + h;

            --最多显示max_show_msg_count条
            loop_cnt = loop_cnt + 1;
            if loop_cnt == max_show_msg_count then
                break;
            end
        end
    end

    right_corner_pop_wnd_view.adjust_wnd_position(y, loop_cnt);
end

function right_corner_pop_wnd_view.adjust_wnd_position(item_height, item_count)
    local ptr =ui_utility.get_ctrl_ptr_by_id(right_corner_pop_wnd_view.item_container_);
    local rc = ptr:GetRectEx(FALSE,TRUE);
    
    local x = rc.left;
    local y = rc.top;

    rc = right_corner_pop_wnd_view.action_btn_container_:GetRectEx(FALSE,TRUE);
    local w = rc.right - rc.left;
    local h = rc.bottom - rc.top;
    rc.left = x;
    rc.right = rc.left + w;
    rc.top = y + item_height;
    rc.bottom = rc.top + h;
    --right_corner_pop_wnd_view.action_btn_container_:MoveEx(rc,TRUE);

    rc = right_corner_pop_wnd_view.dialog_ptr_:GetRectEx(FALSE,TRUE);
    local h = rc.bottom - rc.top - ((3 - item_count) * 60);
    rc.bottom = rc.top + h;
    right_corner_pop_wnd_view.dialog_ptr_:MoveEx(rc,TRUE);

    right_corner_pop_wnd_view.height = rc.bottom - rc.top;
    right_corner_pop_wnd_view.width = rc.right - rc.left;
end

function right_corner_pop_wnd_view.delete_all_sub_items()
    if next(right_corner_pop_wnd_view.id_to_item_) then
        for id, item in pairs(right_corner_pop_wnd_view.id_to_item_) do
            item:delete();
        end
        right_corner_pop_wnd_view.id_to_item_ = {};
    end
end

function right_corner_pop_wnd_view.get_last_message()
    local final_max_six_msg = {};
    local loop_cnt = 0;
    if next(friend_list_model.friend_table_) then
        for index, friend_msg in pairs(friend_list_model.friend_table_) do
            local is_session_opened = talk_dialog_control.is_friend_session_opened(friend_msg["friend_id"]);
            if is_session_opened == false then
                local unread_count = friend_list_model.get_history_unread_msg_count(friend_msg["friend_id"]);
                if unread_count > 0 and friend_list_model.get_last_msg(friend_msg["friend_id"]) ~= nil then
                    local msg_detail = {};
                    msg_detail["count"] = unread_count;
                    msg_detail["type"] = 0; -- 好友
                    msg_detail["id"] = friend_msg["friend_id"];
                    
                    --某些情况下，get_last_msg可能为null
                    local last_msg = friend_list_model.get_last_msg(friend_msg["friend_id"]);
                    local last_msg_in_push = friend_message_model.get_friend_last_message(friend_msg["friend_id"]);    
                    if last_msg == nil and last_msg_in_push ~= nil then
                        base_utility.log("[right_corner_pop_wnd_view] last friend msg is null", true);
                        last_msg = last_msg_in_push;
                    end

                    msg_detail["msg"] = last_msg;
                    local friend_info = friend_list_model.get_friend_info(friend_msg["friend_id"]);
                    msg_detail["face_url"] = friend_info["url"];
                    msg_detail["name"] = friend_info["new_name"] or "";
                    if "" == msg_detail["name"] then
                        msg_detail["name"] = friend_info["name"];
                    end

                    table.insert(final_max_six_msg, msg_detail);
                    loop_cnt = loop_cnt + 1;
                end

                if loop_cnt == 3 then
                    break;
                end
            end
        end
    end

    -- 群未读消息
    loop_cnt = 0;
    if next(group_list_model.group_table_) then
        for index, group_msg in pairs(group_list_model.group_table_) do
            if group_msg.group_type ~= "MatchLiveRoom" and group_msg.group_type ~= "CommonTopicRoom" then
                local is_session_opened = talk_dialog_control.is_group_session_opened(group_msg["group_id"]);
                local mask_flag = group_list_model.get_group_mask_msg_flag(group_msg["group_id"]);
                if is_session_opened == false and mask_flag == 0 then
                    local unread_count = group_list_model.get_group_history_unread_msg_count(group_msg["group_id"]);
                    if unread_count > 0 and group_list_model.get_last_msg(group_msg["group_id"]) ~= nil then
                        local msg_detail = {};
                        msg_detail["count"] = unread_count;
                        msg_detail["type"] = 1; -- 群
                        msg_detail["id"] = group_msg["group_id"];
                        
                        local last_msg = group_list_model.get_last_msg(group_msg["group_id"]);
                        local last_msg_in_push = group_message_model.get_group_last_message(group_msg["group_id"]);                    
                        if last_msg == nil and last_msg_in_push ~= nil then
                            base_utility.log("[right_corner_pop_wnd_view] last group msg is null", true);
                            last_msg = last_msg_in_push;
                        end

                        msg_detail["msg"] = last_msg;
                        local group_info = group_list_model.get_group_info(group_msg["group_id"]);
                        msg_detail["face_url"] = group_info["url"];
                        msg_detail["name"] = group_info["name"];
                        table.insert(final_max_six_msg, msg_detail);
                        loop_cnt = loop_cnt + 1;
                    end

                    if loop_cnt == 3 then
                        break;
                    end
                end
            end
        end
    end

    table.sort(final_max_six_msg, right_corner_pop_wnd_view.sort);

    return final_max_six_msg;
end

function right_corner_pop_wnd_view.sort(a, b)
    -- body
    if a.msg ~= nil and b.msg ~= nil then 
        local time_a = a.msg.time;
        local time_b = b.msg.time;
        return time_a > time_b;
    else
        base_utility.log("right_corner_pop_wnd_view.sort error!!", true);
        return true;
    end
end


function right_corner_pop_wnd_view.start_show_msg_box_timer()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('right_corner_pop_wnd_view.open_msg_box_process');
    tpfui:AddInterval('right_corner_pop_wnd_view.open_msg_box_process', 500);
end

function right_corner_pop_wnd_view.open_msg_box_process()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('right_corner_pop_wnd_view.open_msg_box_process');
    right_corner_pop_wnd_view.open_meesage_box();
end

function right_corner_pop_wnd_view.click_view_all(uilib, ctrl_id)
    right_corner_pop_wnd_view.hide_immediately();
    local unread_friend_list = tray_manager_model.get_unread_msg_friend_id_list();
    local unread_group_list = tray_manager_model.get_unread_msg_group_id_list();
    if next(unread_friend_list) or next(unread_group_list) then
        tray_manager_control.open_all_talk_session();
    end

    tqos_mgr.increase_tray_hover_wnd_click_view_all_count(); 
end

function right_corner_pop_wnd_view.click_ignore_all(uilib, ctrl_id)
    right_corner_pop_wnd_view.hide_immediately();
    tray_manager_control.ingore_all_flash_except_preload();

    tqos_mgr.increase_tray_hover_wnd_click_ingore_count(); 
end

function right_corner_pop_wnd_view.click_on_msg_item(uilib, ctrl_id)
    right_corner_pop_wnd_view.hide_immediately();
    local s_ptr =ui_utility.get_ctrl_ptr_by_id(ctrl_id);
    local p_ptr = s_ptr:GetParent();
    local p_id = p_ptr:GetControlID();

    local item = right_corner_pop_wnd_view.get_item_from_id(p_id);
    if item then
        local item_type = item:get_itme_type();
        if item_type == pop_wnd_item.TALK_MSG_TYPE then
            tqos_mgr.increase_tray_hover_wnd_click_on_talk_msg_count(); 
            local msg_item_type, id = item:get_talk_type_and_id();
            if msg_item_type == 0 then -- 好友
                right_corner_pop_wnd_view.show_friend_talk(id);
            elseif msg_item_type == 1 then -- 群
                right_corner_pop_wnd_view.show_group_talk(id);
            else
            end
        else
            --打开消息盒子
            tqos_mgr.increase_tray_hover_wnd_click_on_msg_box_count(); 
            right_corner_pop_wnd_view.open_meesage_box();
        end
    end
end

function right_corner_pop_wnd_view.open_meesage_box()
    msg_box_frame_control.show_report_count_flag(0);
    -- message_box_control.show(nil, nil);
    event_center.send_event(group_event.SHOW_MESSAGE_BOX_DIALOG, "");
end

function right_corner_pop_wnd_view.show_friend_talk(friend_id)
    if pwg_social_control:is_new_social_enabled() then
        local param = {
            session_type = 1;
            target_id = friend_id;
        };
        comm_center.call_service("i_service_new_social_open_panel", param);
    else
        local table_param = {};
        local friends = {};
        local info = {};
        info.id = friend_id;
        info.is_new = false;
        ierd_tgp.tpf_ui.Require("social\\friend_list\\model.lua")
        info.time = friend_list_model.get_last_msg_time(info.id);
        table.insert(friends, info);
        table_param["friends"] = friends;
        event_center.send_event(group_event.SHOW_TALK_DIALOG,
                                base_utility.table_to_json(table_param));
    end
end

function right_corner_pop_wnd_view.show_group_talk(group_id)
    local table_param = {};
    local groups = {};
    local info = {};
    info.id = group_id;
    info.is_new = false;
    ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
    info.time = group_list_model.get_last_msg_time(info.id); 
    info.group_type = group_list_model.get_group_type(info.id);   
    table.insert(groups, info);
    table_param["groups"] = groups;
    event_center.send_event(group_event.SHOW_TALK_DIALOG,base_utility.table_to_json(table_param));
end

function right_corner_pop_wnd_view.get_item_from_id(ctrl_id)
    if next(right_corner_pop_wnd_view.id_to_item_) then
        for id, item in pairs(right_corner_pop_wnd_view.id_to_item_) do
            if id == ctrl_id then
                return item;
            end
        end
    end
end

right_corner_pop_wnd_view.init();

